﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsFieldAttr
    {
        private readonly KeRui.DAL.CmsFieldAttr dal = new KeRui.DAL.CmsFieldAttr();

        public int Add(KeRui.Model.CmsFieldAttr model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsFieldAttr> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsFieldAttr> list = new List<KeRui.Model.CmsFieldAttr>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsFieldAttr item = new KeRui.Model.CmsFieldAttr();
                    if (dt.Rows[i]["AttributeID"].ToString() != "")
                    {
                        item.AttributeID = int.Parse(dt.Rows[i]["AttributeID"].ToString());
                    }
                    if (dt.Rows[i]["ParentID"].ToString() != "")
                    {
                        item.ParentID = int.Parse(dt.Rows[i]["ParentID"].ToString());
                    }
                    item.ClassName = dt.Rows[i]["ClassName"].ToString();
                    item.AttributeName = dt.Rows[i]["AttributeName"].ToString();
                    item.AttributeValue = dt.Rows[i]["AttributeValue"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(string className)
        {
            this.dal.Delete(className);
        }

        public DataTable GetClassList()
        {
            return this.dal.GetClassList();
        }

        public List<KeRui.Model.CmsFieldAttr> GetList(string strWhere)
        {
            string strOrder = "AttributeValue ASC";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsFieldAttr", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsFieldAttr> GetListByCache(string strCondition)
        {
            string cacheKey = "CmsFieldAttrList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsFieldAttr>) cache;
        }

        public KeRui.Model.CmsFieldAttr GetModel(int AttributeID)
        {
            return this.dal.GetModel(AttributeID);
        }

        public void Update(KeRui.Model.CmsFieldAttr model)
        {
            this.dal.Update(model);
        }
    }
}

